package com.gaussian.property.admin.module.biz.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gaussian.property.module.biz.entity.*;
import com.gaussian.property.module.biz.mapper.ProjectReportFileMapper;
import com.gaussian.property.module.biz.service.IProjectReportFileService;
import com.gaussian.property.module.biz.service.IProjectService;
import com.gaussian.property.module.biz.vo.projectreport.AddProjectReport;
import com.gaussian.property.module.biz.vo.projectreport.PageQueryProjectReportRequest;
import com.gaussian.property.module.biz.vo.projectreport.ProjectReportFileVO;
import com.gaussian.property.module.system.entity.SysUser;
import com.gaussian.property.module.system.service.ISysUserService;
import com.songqi.common.core.exception.CustomException;
import com.songqi.common.mybatis.core.util.MyBatisUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: songqi(13776346982)
 * @date: 2024/8/11 13:19
 */
@Service
public class ProjectReportService {

    @Resource
    private IProjectReportFileService iProjectReportFileService;
    @Resource
    private ProjectReportFileMapper projectReportFileMapper;
    @Resource
    private IProjectService iProjectService;
    @Resource
    private ISysUserService iSysUserService;


    public IPage<ProjectReportFileVO> pageQueryProjectReport(PageQueryProjectReportRequest queryRequest) {
        IPage<ProjectReportFileVO> iPage = MyBatisUtils.buildPage(queryRequest);
        IPage<ProjectReportFileVO> pageData = projectReportFileMapper.pageQueryProjectReport(iPage, queryRequest);
        if(CollUtil.isNotEmpty(pageData.getRecords())){
            List<String> projectIdList = pageData.getRecords().stream().map(ProjectReportFileVO::getProjectId).distinct().collect(Collectors.toList());
            List<String> createByList = pageData.getRecords().stream().map(ProjectReportFileVO::getCreateBy).distinct().collect(Collectors.toList());
            List<String> updateByList = pageData.getRecords().stream().map(ProjectReportFileVO::getUpdateBy).distinct().collect(Collectors.toList());
            List<String> userNameList = new ArrayList<>();
            userNameList.addAll(createByList);
            userNameList.addAll(updateByList);
            userNameList = userNameList.stream().distinct().collect(Collectors.toList());
            Map<String, SysUser> appUserList = iSysUserService.selectUserMap(userNameList);
            List<Project> projectList = listProjectByProjectId(projectIdList);
            Map<String, Project> projectMapByProjectId = projectList.stream().collect(Collectors.toMap(Project::getProjectId, Function.identity()));
            for (ProjectReportFileVO projectReportFileVO : pageData.getRecords()) {
                Project project = projectMapByProjectId.get(projectReportFileVO.getProjectId());
                if(project != null){
                    projectReportFileVO.setProjectName(project.getProjectName());
                }
                SysUser createAppUser = appUserList.get(projectReportFileVO.getCreateBy());
                if(createAppUser != null){
                    projectReportFileVO.setCreatorRealName(createAppUser.getRealName());
                }
                SysUser updateAppUser = appUserList.get(projectReportFileVO.getUpdateBy());
                if(updateAppUser != null){
                    projectReportFileVO.setUpdateRealName(updateAppUser.getRealName());
                }
            }
        }
        return iPage;
    }

    /**
     * 报告上传
     * @param addProjectReport
     */
    public void addProjectReport(AddProjectReport addProjectReport) {
        iProjectReportFileService.addProjectReport(addProjectReport);
    }

    /**
     *
     * @param projectReportFileId
     * @return
     */
    public Integer changeStatus(String projectReportFileId) {
        ProjectReportFile projectReportFile = iProjectReportFileService.getById(projectReportFileId);
        if(projectReportFile == null){
            throw new CustomException("【参数错误】项目报告id不存在");
        }
        int status = projectReportFile.getIsDelete() ^ 1;
        projectReportFile.setIsDelete(status);
        iProjectReportFileService.updateById(projectReportFile);
        return status;
    }

    private List<Project> listProjectByProjectId(List<String> projectIdList) {
        LambdaQueryWrapper<Project> queryWrapper = new LambdaQueryWrapper<Project>()
                .in(Project::getProjectId, projectIdList);
        return iProjectService.list(queryWrapper);
    }



}
